<?php
/**
 * Yotaku Shop - yshop
 * @author Creatint<creatint@163.com>
 * @copyright www.yotaku.cn 2016
 */
namespace Admin\Model;
use Think\Model;
class OrderModel extends Model{
    public $error   =   '';
    //自动完成
    protected $_auto = array(
        array('update_at', NOW_TIME, self::MODEL_BOTH),
    );


    public function getOrder($params=null,$page=false){

        //初始化查询条件
        $map        =   array();
        //商品名称
        if(!empty($params['title'])){
            $map['cartdata']   =   array('like', '%'.utf8_str_to_unicode($params['title']).'%');
        }
        //订单号
        if(!empty($params['number'])){
            $map['number']   =   array('like', '%'.(string)$params['number'].'%');
        }
        //总价
        if(!empty($params['total_price'])){
            $arr_tmp    =   explode(',',$params['total_price']);
            $map['total_price']   =   array('between',$arr_tmp);
            unset($arr_tmp);
        }
        //支付方式
        if($params['pay_type']>-1){
            $map['pay_type']   =   $params['pay_type'];
        }
        //支付状态
        if($params['pay_status']>-1){
            $map['pay_status']   =   $params['pay_status'];
        }
        //配送方式
        if($params['deliver_type']>-1){
            $map['deliver_type']   =   $params['deliver_type'];
        }
        //配送状态
        if($params['deliver_status']>-1){
            $map['deliver_status']   =   $params['deliver_status'];
        }
        //订单状态
        if($params['status']>-1){
            $map['status']   =   $params['status'];
        }

        
        $result =   $this->where($map)->order("update_at desc, time_end desc")->select();
        return $result;
    }
    public function update($data){

        if($data['id']){
            if($data = $this->create($data) && $this->where("id={$data['id']}")->save()){
                return $data;
            }
            $this->error = '更新失败';
        }else{
            if(($data=$this->create($data)) && ($data['id']=$this->add($data))){
                return $data;
            }
            $this->error = '新增失败';
        }
    }

    //删除
    public function del($id){
        if(!$id){
            $this->error = '无ID';
            return false;
        }

        if(is_array($id)){
            $map['id']  =   array('in',$id);
        }elseif(is_numeric($id)){
            $map['id']  =   $id;
        }elseif($id = explode(',',$id)){
            $map['id']  =   array('in',$id);
        }
        $result =   $this->where($map)->delete();
        if(!$result){
            $this->error = '删除失败';
            return false;
        }
        return $result;
    }
    //获取错误
    public function getError(){
        return $this->error;
    }
}